package com.zyit.dao;

import com.zyit.entity.T_notice;
import com.zyit.util.BeanRowMapper;
import com.zyit.util.DBHelp;
import com.zyit.util.MapRowMapper;
import com.zyit.util.Page;

import java.util.List;
import java.util.Map;

public class NewsDao {
    private DBHelp dbHelp = new DBHelp();

    public List<T_notice> findAll() {
        String sql = "select id, title,create_time from t_notice";
        return dbHelp.executeQueryForList(sql,new BeanRowMapper<>(T_notice.class));
    }


    public Page<T_notice> findByPage(Integer pageNo) {
        Integer count = count();
        Page<T_notice> page = new Page<>(pageNo,count);
        String sql = "select id, title,create_time from t_notice limit ?,?";
        List<T_notice> t_noticeList = dbHelp.executeQueryForList(sql, new BeanRowMapper<>(T_notice.class), page.getStart(), page.getPageSize());
        page.setPageList(t_noticeList);

        return page;

    }
    private Integer count() {
        String sql = "select count(*) from t_notice";
        Map<String,Object> map = dbHelp.executeQueryForObject(sql,new MapRowMapper());
        Long count = (Long) map.get("count(*)");
        return count.intValue();
    }
    public List<T_notice> findByContent(String content) {

        String sql = "select title,create_time where t_notice  like ?";
        return dbHelp.executeQueryForList(sql,new BeanRowMapper<>(T_notice.class),"%"+content+"%");
    }

    public List<T_notice> findPage(Integer page) {
        String sql = "select title,create_time from t_notice limit ?,?";
        return dbHelp.executeQueryForList(sql,new BeanRowMapper<>(T_notice.class),(page-1)*5,5);
    }

    public List<T_notice> findDetail(String id) {

        String sql = "select id, title,content from t_notice where id=?";
        return dbHelp.executeQueryForList(sql,new BeanRowMapper<>(T_notice.class),id);
    }
}
